/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.patpro.controladores;


import com.patpro.modelo.javabeans.Alumno;
import com.patpro.modelo.servicios.AlumnoServicio;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

/** 
 
 */
public class ActualizaAlumnoController extends SimpleFormController {

    private AlumnoServicio servicio_alumno;

    public ActualizaAlumnoController() {
        setCommandClass(Alumno.class);
        setCommandName("alumnoBean");
        //setSuccessView("successView");
        //setFormView("formView");
    }

   

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        Alumno c = (Alumno) super.formBackingObject(request);
        String id = request.getParameter("id");
        if(id == null){
            c.setCodigoUniversitario("051YYYYXXX");
            c.setSemestreIngreso("YYYYS");
        }
        else{
            c = getServicio_alumno().buscarAlumno(Integer.parseInt(id));
        }

        return c;
    }

    @Override
    public Map referenceData(HttpServletRequest request) {
        Map modelo = new HashMap();
        List<Alumno> alumno = getServicio_alumno().listarAlumnos();
        modelo.put("lista_alumnos", alumno);

        String id = request.getParameter("id");
        if(id == null)
            modelo.put("opcion", "Agregar");
        else
            modelo.put("opcion", "Actualizar");
        return modelo;
    }

    @Override
    protected ModelAndView onSubmit(
            HttpServletRequest request,
            HttpServletResponse response,
            Object command,
            BindException errors) throws Exception {
        ModelAndView mv =null;

        Alumno alumno = (Alumno) command;
        int resp = getServicio_alumno().actualizarAlumno(alumno);
        if(resp > 0)
            mv = new ModelAndView(getSuccessView());
        else
            mv = new ModelAndView("error");
        mv.addObject("controlador", "adminAlumno");
        return mv;
    }

    /**
     * @return the servicio_alumno
     */
    public AlumnoServicio getServicio_alumno() {
        return servicio_alumno;
    }

    /**
     * @param servicio_alumno the servicio_alumno to set
     */
    public void setServicio_alumno(AlumnoServicio servicio_alumno) {
        this.servicio_alumno = servicio_alumno;
    }
}
